package Messages;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ocsf.server.ConnectionToClient;
import server.SQLConnection;
/**
 * This class implements the Add supplier to DB
 * @author Gabi Revah
 * @author Aviad Almog
 */
public class MessageAddSupplier {

	private ConnectionToClient client ;
	private String[] str;
	static int row1;
/**
 * Constructor of this class 
 * @param str
 * @param client
 */
public MessageAddSupplier(String[] str, ConnectionToClient client){
		
		this.client= client ;
		this.str= str; 	
	}
	
	/**
	 * Adding supplier to the DB 
	 * @throws SQLException
	 * @throws IOException
	 */
	public void AddSupplier() throws SQLException, IOException{
	
				SQLConnection sc = new SQLConnection();
				java.sql.PreparedStatement sid = sc.getConnection().prepareStatement("SELECT * FROM db.suppliers;");
				java.sql.PreparedStatement Supps = sc.getConnection().prepareStatement("INSERT INTO db.suppliers (idsuppliers,SuppliersName,Address,PhoneNumber,ContactMan,ContactPhone) VALUES(?,?,?,?,?,?);");	
				
				ResultSet rs= sid.executeQuery();
				row1 = 0;
				while(rs.next()){
					int temp = rs.getInt(1);
						if(row1>temp)
							row1=row1;
							else{
							row1=temp;}
	
				}
										Supps.setInt(1, this.row1+1);
										Supps.setString(2,str[1]);
										Supps.setString(3,str[2]);
										Supps.setString(4,str[3]);
										Supps.setString(5,str[4]);
										Supps.setString(6,str[5]);
										Supps.executeUpdate();
										Supps.close();
					
										ArrayList<Object> Sup=new ArrayList<Object>();
										Sup.add("AddSuppl");
										Sup.add(this.row1+1);
										client.sendToClient(Sup);

}
}